
#include	<iostream>
#include	<cstdlib>
#include	"GUI.hh"

GUI::GUI(void)
  : info(), running(true), focus(MAINMENU), dataS(), action(),
    App(sf::VideoMode(dataS.getW(), dataS.getH(), 32), dataS.getName().c_str()), timeA(0), timeB(0)
{
  this->App.SetFramerateLimit(dataS.getFps());
  this->action.push_back(new MainMenu(this->App, this->focus, this->Event));
  this->action.push_back(new OptionMenu(this->App, this->focus, this->Event, this->info));
  this->action.push_back(new ConnectMenu(this->App, this->focus, this->Event, this->info));
  this->action.push_back(new Pvp(this->App, this->focus, this->Event, this->info));
  this->action.push_back(new PvpW(this->App, this->focus, this->Event, this->info));
  this->action.push_back(new PvpB(this->App, this->focus, this->Event, this->info));
}

GUI::~GUI(void)
{
  while (!this->action.empty())
    this->action.pop_back();
  this->App.Close();
}

bool		GUI::run(void)
{
  bool		e;

  if (this->Event_.Type == sf::Event::KeyPressed)
    {
      if (this->focus != IGUI::EXIT)
	this->action[this->focus]->run();
      else
      	this->stop();
      this->App.Display();
      this->Event_.Type = sf::Event::MouseMoved;
    }
  else 
    {
      e = this->App.GetEvent(this->Event);
      if (this->focus != IGUI::EXIT)
	this->action[this->focus]->run();
      else
	this->stop();
      this->App.Display();
    }
  if (e)
    while (this->App.GetEvent(this->Event) && this->Event.Type == sf::Event::MouseMoved);
  this->Event_ = this->Event;
  return (this->running); 
}

void		GUI::stop(void)
{
  this->running = false;
}
